<?php

/**
 * @var $submit Zend_Form_Element_Submit
 */
class Contest_Form_Basic extends Contest_Form_Abstract
{
    protected function _initBegin()
	{
	}
	protected  function _hasBackButton() {
		return false;
	}
	protected function _initFields()
	{
		$this->_initFanpage();

		$this->addElement('text', 'title', array(
			'label'		=> $this->getTranslator()->_('CONTEST_NAME'),
            'class'     => 'length_longest',
			'required'	=> true,
			'filters'	=> array('StringTrim')
		));
		$maxLengthValidator = new Zend_Validate_StringLength(array('max' => 255));
		$maxLengthValidator->setMessage('ERROR_CONTEST_NAME_IS_TOO_LONG',Zend_Validate_StringLength::TOO_LONG);
		$this->title->addValidator($maxLengthValidator);

		$this->addElement('RichEditor', 'description', array(
			'label'		=> $this->getTranslator()->_('CONTEST_DESCRIPTION'),
            'class'     => 'length_longest',
			'rows'		=> '3',
			'required'	=> true
		));
        $this->description->addFilter(new YouNet_Filter_Purifier());
		$validatorNotEmpty = new YouNet_Validate_NotEmpty(array('type' => YouNet_Validate_NotEmpty::RICHTEXT));
		$this->description->addValidator($validatorNotEmpty);

		$this->addElement('multiCheckbox','options',array(
			'label'			=> $this->getTranslator()->_('OPTIONS_SETTING_LABEL'),
			'multiOptions'	=> array(
				'is_wait_for_approve'		=> $this->getTranslator()->_('BASIC_OPTION_WAIT_FOR_APPOVED'),
				'is_allow_comment'			=> $this->getTranslator()->_('BASIC_OPTION_ALLOW_COMMENT'),
				'is_allow_multi_vote'		=> $this->getTranslator()->_('BASIC_OPTION_ALLOW_MULTI_VOTE'),
				'is_allow_multi_submit'		=> $this->getTranslator()->_('BASIC_OPTION_ALLOW_MULTI_SUBMIT'),
			),
			'value' => array(
                'is_wait_for_approve',
				'is_allow_multi_vote',
				'is_allow_comment',
				'is_allow_multi_submit'
			)
		));
        
        //advance setting
        $this->addElement('multiCheckbox','advance_options',array(
			'label'			=> $this->getTranslator()->_('ADVANCE_SETTING_LABEL'),
			'multiOptions'	=> array(
                'is_two_column'             => $this->getTranslator()->_('ADVANCE_OPTION_DISPLAY_ENTRY_TWO_COLUMN').' <a id="preview_two_col_entry" class="link" >(Preview)</a>',
				'is_show_social_like_btns'	=> $this->getTranslator()->_('ADVANCE_OPTION_DISPLAY_LIKE_BUTTON_ENTRY').' <a id="preview_social_like" class="link" >(Preview)</a>',
				'is_auto_post'              => 'ADVANCE_OPTION_AUTO_SHARE_AFTER_VOTE'
			),
			'value' => array(
                'is_two_column',
                'is_show_social_like_btns'
			),
            'escape' => false
		));
        
		$this->addElement('RichEditor','template_congratulation',array(
			'label' => $this->getTranslator()->_('THEME_TEMPLATE_CONGRATULATION'),
            'class'     => 'length_longest',
			'rows'		=> '3',
		));
	}

	private function _initFanpage()
	{
		$this->addElement('MultiCheckbox', self::FANPAGE_NAME, array(
			'label'=> $this->getTranslator()->_('FANPAGES'),
			'required'	=> true
		));
		$this->getElement(self::FANPAGE_NAME)->setMultiOptions($this->_social->getPageList());
		$this->getElement(self::FANPAGE_NAME)->setDecorators(array(array('ViewScript', array(
			'viewScript' => 'decorator/load_my_fanpages.phtml',
		))));
	}

	/**
	 *
	 * @param Core_Model_Contest $contestRow
	 * @return Contest_Form_Basic
	 */
	public function fill(Core_Model_Contest $contestRow)
	{
		$data		= $contestRow->toArray();
		$optionData = array('options' => array());
		foreach( array('is_wait_for_approve','is_allow_comment','is_allow_multi_vote','is_allow_multi_submit') as $option )
		{
			if( $data[$option] == 1)
			{
				$optionData['options'][] = $option;
			}
		}
        
        //fill advance setting
        $optionAdvanceData = array('advance_options' => array());
        foreach( array('is_two_column', 'is_auto_post','is_show_social_like_btns') as $advance_option )
		{
			if( $data[$advance_option] == 1)
			{
				$optionAdvanceData['advance_options'][] = $advance_option;
			}
		}
       
        
		$this->populate(array_merge(
			$data,
			$optionData,
            $optionAdvanceData,
			array(self::FANPAGE_NAME => $contestRow->getArrIdPages() )
		));
		return $this;
	}

	public static function GetOptionsArray($isLimitted = false)
	{
        return array(
            //basic options
            'is_wait_for_approve',
            'is_allow_comment',
            'is_allow_multi_vote',
            'is_allow_multi_submit',
            
            // advance options
            'is_two_column',
            'is_show_social_like_btns',
            'is_auto_post'
        );
	}

	public function disableFields()
	{
		//@note: do not remove this line
		//$this->options->setIgnore(true);//It does not work, it will ignore all, but we just want some
		$this->options->setRequired(false);
		/*
		$this->{self::FANPAGE_NAME}->setAttrib('disable',$this->_social->getUserPageIds());
		$this->{self::FANPAGE_NAME}->setIgnore(true);
		$this->{self::FANPAGE_NAME}->setRequired(false);
		*/
	}
    public function litmitEditFanpage()
	{
		$arrUserPageIds			= YouNet_Api::getInstance()->getSocialPlugin()->getUserPageIds();
        if(!empty($arrUserPageIds)){
            $arrPublishedPageIds	= Core_Model_DbTable_Pages::getOtherPublishedPageIds($this->_contestRow->id, $arrUserPageIds);
            $this->{self::FANPAGE_NAME}->setAttrib('disable', $arrPublishedPageIds);
        }
	}
	public function render(Zend_View_Interface $view = null)
	{
		$dataToPopulate = $_POST;
		if( $this->_contestRow instanceof Core_Model_Contest )
		{
			if( !$this->_contestRow->canEditFanpage() )
			{
                if( $this->_contestRow->isLimitEditFanpage() )
                {
                    $this->litmitEditFanpage();
                }
			}
		}
		if( isset($_POST['form_submitted']) )
		{
			if( !($this->_contestRow instanceof Core_Model_Contest) )
			{
				throw new Exception('Need to set _contestRow to populate disable value');
			}
		}
		$this->populate($dataToPopulate);
		/*
		 * remove button cancel and back
	     */
        $this->removeButton();
		return parent::render($view);
	}
};